import 'package:flutter/material.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';

class TransactionListPage extends StatefulWidget {
  const TransactionListPage({super.key});

  @override
  State<TransactionListPage> createState() => _TransactionListPageState();
}

class _TransactionListPageState extends State<TransactionListPage> {
  final RefreshController _refreshController = RefreshController();

  // 模拟交易记录
  final List<Map<String, dynamic>> _transactions = [
    {
      'id': '1',
      'type': 'income',
      'amount': 320.00,
      'title': '搬家服务收入',
      'orderId': 'DD20240320001',
      'time': '2024-03-20 15:30',
      'status': 'completed',
    },
    {
      'id': '2',
      'type': 'withdraw',
      'amount': 1000.00,
      'title': '提现',
      'time': '2024-03-19 14:20',
      'status': 'completed',
    },
    {
      'id': '3',
      'type': 'income',
      'amount': 280.00,
      'title': '搬家服务收入',
      'orderId': 'DD20240319001',
      'time': '2024-03-19 10:15',
      'status': 'completed',
    },
  ];

  @override
  void dispose() {
    _refreshController.dispose();
    super.dispose();
  }

  Future<void> _onRefresh() async {
    try {
      await Future.delayed(const Duration(seconds: 1));
      _refreshController.refreshCompleted();
    } catch (e) {
      _refreshController.refreshFailed();
    }
  }

  Future<void> _onLoading() async {
    try {
      await Future.delayed(const Duration(seconds: 1));
      setState(() {
        _transactions.addAll([
          {
            'id': DateTime.now().toString(),
            'type': 'income',
            'amount': 260.00,
            'title': '搬家服务收入',
            'orderId': 'DD20240318001',
            'time': '2024-03-18 16:40',
            'status': 'completed',
          }
        ]);
      });
      _refreshController.loadComplete();
    } catch (e) {
      _refreshController.loadFailed();
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: const Color(0xFFF5F5F5),
      appBar: AppBar(
        title: const Text('交易记录'),
        centerTitle: true,
        backgroundColor: Colors.white,
        elevation: 0,
      ),
      body: SmartRefresher(
        controller: _refreshController,
        enablePullDown: true,
        enablePullUp: _transactions.length >= 10,
        header: ClassicHeader(
          refreshStyle: RefreshStyle.Follow,
          idleText: '下拉刷新',
          refreshingText: '加载中...',
          completeText: '刷新成功',
          failedText: '刷新失败',
          releaseText: '松开刷新',
          height: 44,
          spacing: 8,
          iconPos: IconPosition.left,
          textStyle: TextStyle(color: Colors.grey[600], fontSize: 12),
          refreshingIcon: SizedBox(
            width: 16,
            height: 16,
            child: CircularProgressIndicator(
              strokeWidth: 2,
              valueColor: AlwaysStoppedAnimation<Color>(Colors.grey[600]!),
            ),
          ),
        ),
        footer: ClassicFooter(
          loadStyle: LoadStyle.ShowAlways,
          idleText: '上拉加载更多',
          loadingText: '加载中...',
          noDataText: '没有更多数据',
          failedText: '加载失败',
          canLoadingText: '松开加载更多',
          height: 44,
          spacing: 8,
          iconPos: IconPosition.left,
          textStyle: TextStyle(color: Colors.grey[600], fontSize: 12),
          loadingIcon: SizedBox(
            width: 16,
            height: 16,
            child: CircularProgressIndicator(
              strokeWidth: 2,
              valueColor: AlwaysStoppedAnimation<Color>(Colors.grey[600]!),
            ),
          ),
        ),
        onRefresh: _onRefresh,
        onLoading: _onLoading,
        child: ListView.builder(
          padding: const EdgeInsets.all(16),
          itemCount: _transactions.length,
          itemBuilder: (context, index) {
            final transaction = _transactions[index];
            return Container(
              margin: const EdgeInsets.only(bottom: 12),
              decoration: BoxDecoration(
                color: Colors.white,
                borderRadius: BorderRadius.circular(8),
              ),
              child: Material(
                color: Colors.transparent,
                child: InkWell(
                  onTap: () {
                    // TODO: 查看交易详情
                  },
                  borderRadius: BorderRadius.circular(8),
                  child: Padding(
                    padding: const EdgeInsets.all(16),
                    child: Row(
                      children: [
                        Container(
                          width: 40,
                          height: 40,
                          decoration: BoxDecoration(
                            color: transaction['type'] == 'income'
                                ? const Color(0xFFFF5722).withOpacity(0.1)
                                : Colors.blue.withOpacity(0.1),
                            shape: BoxShape.circle,
                          ),
                          child: Icon(
                            transaction['type'] == 'income'
                                ? Icons.account_balance_wallet
                                : Icons.payment,
                            color: transaction['type'] == 'income'
                                ? const Color(0xFFFF5722)
                                : Colors.blue,
                            size: 20,
                          ),
                        ),
                        const SizedBox(width: 12),
                        Expanded(
                          child: Column(
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: [
                              Text(
                                transaction['title'],
                                style: const TextStyle(
                                  fontSize: 15,
                                  fontWeight: FontWeight.w500,
                                ),
                              ),
                              const SizedBox(height: 4),
                              Text(
                                transaction['time'],
                                style: TextStyle(
                                  fontSize: 12,
                                  color: Colors.grey[600],
                                ),
                              ),
                              if (transaction['orderId'] != null) ...[
                                const SizedBox(height: 4),
                                Text(
                                  '订单号：${transaction['orderId']}',
                                  style: TextStyle(
                                    fontSize: 12,
                                    color: Colors.grey[600],
                                  ),
                                ),
                              ],
                            ],
                          ),
                        ),
                        Text(
                          transaction['type'] == 'income'
                              ? '+${transaction['amount'].toStringAsFixed(2)}'
                              : '-${transaction['amount'].toStringAsFixed(2)}',
                          style: TextStyle(
                            fontSize: 16,
                            fontWeight: FontWeight.w500,
                            color: transaction['type'] == 'income'
                                ? const Color(0xFFFF5722)
                                : Colors.blue,
                          ),
                        ),
                      ],
                    ),
                  ),
                ),
              ),
            );
          },
        ),
      ),
    );
  }
} 